# coding: utf-8

import os
import conf


def install_helm3(helm_version):
    helm_pkg = 'helm-%s-linux-amd64.tar.gz' % helm_version
    info = """
    ---------------------------------------------------------------------------
    helm repo add stable http://mirror.azure.cn/kubernetes/charts
    helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
    helm repo update
    ===========================================================================
    """
    format_print(string='正在安装helm，请耐心等待...')
    os.system("mkdir -p /opt/helm")
    # 下载源码包
    os.system("wget -P /opt/helm https://get.helm.sh/%s > /dev/null 2>&1" % helm_pkg)
    # 解压tar -zxvf /opt/helm/helm-v3.7.2-linux-amd64.tar.gz > /dev/null 2>&1
    os.system("cd /opt/helm;tar -zxvf /opt/helm/%s > /dev/null 2>&1" % helm_pkg)
    # 在解压目中找到helm程序，移动到需要的目录中(mv linux-amd64/helm /usr/local/bin/helm)
    os.system("mv /opt/helm/linux-amd64/helm /usr/local/bin/helm")
    format_print(string='helm install successfully',mode=False)
    format_print(string='使用helm version 命令查看版本',mode=False)


    # 指定k8s集群位置
    os.system("export KUBECONFIG=/root/.kube/config ")  # 可以写到/etc/profile里
    os.system("sed -i '/export/a\export KUBECONFIG=/root/.kube/config' /etc/profile ")
    os.system("source /etc/profile")
    format_print(string='可以执行以下命令添加共有仓库',mode=False)
    print("\033[32;1m%s\033[0m" % info)


def format_print(p=None,string='test', mode=True):
    if mode:
        print("\033[34;1m*\033[0m" * 20, "\033[34;1m%s\033[0m" % string)
    else:
        print("\033[32;1m*\033[0m" * 20, "\033[32;1m%s\033[0m" % string)


if __name__ == '__main__':
    install_helm3(conf.HELM_VERSION)
    format_print()
